package com.jr.controller;

/**
 * @Function：
 * @Author 梁明哲
 * @Email 424577090@qq.com
 * @Package ${PACKAGE_NAME}
 * @Project InvoiceManage
 * @Date 2023-02-20 15:31:20
 */

import com.alibaba.fastjson.JSON;
import com.jr.entity.BaseData;
import com.jr.service.BaseDataService;
import com.jr.service.impl.BaseDataServiceImpl;
import com.jr.util.Message;
import com.jr.util.ResultUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

@WebServlet("*.bd")
@MultipartConfig
public class BaseDataServlet extends HttpServlet {
    private BaseDataService baseDataService = new BaseDataServiceImpl();
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String servletPath = request.getServletPath();
        String methodName = servletPath.substring(servletPath.lastIndexOf("/") + 1,servletPath.length() - 3);

        try {
            Method method = getClass().getDeclaredMethod(methodName, HttpServletRequest.class, HttpServletResponse.class);
            method.invoke(this,request,response);
        } catch (NoSuchMethodException e) {
            throw new RuntimeException(e);
        } catch (InvocationTargetException e) {
            throw new RuntimeException(e);
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request,response);
    }

    /**
     * 根据企业id查询企业相关信息并展示到前端页面
     * @param request 前端的请求
     * @param response 响应给前端
     * @throws ServletException
     * @throws IOException
     */
    private void updateBaseData(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
        PrintWriter out = response.getWriter();
        try {
            Integer enterpriseId = (Integer) request.getSession().getAttribute("enterpriseId");
            BaseData baseData = baseDataService.findOne(enterpriseId);
            out.write(JSON.toJSONString(new ResultUtil("baseData",baseData)));
        } catch (Exception e) {
            throw new RuntimeException(e);
        }finally {
            out.close();
        }
    }

    /**
     * 根据企业id更新发票信息管理
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    private void saveBaseData(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
        PrintWriter out = response.getWriter();
        String bankName = request.getParameter("bankName");
        String bankAccount= request.getParameter("bankAccount");
        String phone = request.getParameter("phone");
        String address = request.getParameter("address");
        //System.out.println(bankName);
        BaseData baseData = new BaseData((Integer) request.getSession().getAttribute("enterpriseId"),bankName,bankAccount,phone,address);
        try {
            Integer updateBaseData = baseDataService.updateBaseData(baseData);
            if(updateBaseData>0){
                out.write(JSON.toJSONString(new Message(300,"更新成功")));
            } else {
                out.write(JSON.toJSONString(new Message(301,"更新失败")));
            }
            out.flush();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }finally {
            out.close();
        }
    }

    /**
     * 根据企业id将数据回显
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    private void listBaseData(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
        PrintWriter out = response.getWriter();
        try {
            BaseData data = baseDataService.findOne((Integer) request.getSession().getAttribute("enterpriseId"));
            out.write(JSON.toJSONString(new ResultUtil("data",data)));
        } catch (Exception e) {
            throw new RuntimeException(e);
        }finally {
            out.close();
        }
    }

    /**
     * 获取当前登录的用户的姓名
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    private void getUserName(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
        PrintWriter out = response.getWriter();
        String name = (String)request.getSession().getAttribute("name");
        out.write(JSON.toJSONString(new Message(name)));
    }


}
